/*
结构体

2021-10-04
*/

package ding

import (
	"encoding/json"
	"fjj/go/ucommon"
	"fmt"
)

//TODO:获取ACCESSTOKEN,错误返回空字符
//钉钉请求限制，每秒40次

func GetAccessToken(appKey, appSecret string) string {

	url := fmt.Sprintf("https://oapi.dingtalk.com/gettoken?appkey=%s&appsecret=%s",
		appKey, appSecret)

	s := ucommon.Get(&url)
	println("企业内部应用的access_token BODY：", s)

	m := access_token_back{}
	err := json.Unmarshal([]byte(s), &m)
	if err != nil {
		fmt.Println("AccessToken JSON Umarshal failed:", err)
		return ""
	}

	return m.AccessToken
}

// https://developers.dingtalk.com/document/app/obtain-orgapp-token
type access_token_back struct {
	Errcode     int    `json:"errcode"`
	AccessToken string `json:"access_token"`
	Errmsg      string `json:"errmsg"`
	ExpiresIn   int    `json:"expires_in"`
}

// 企业员工人数返回信息
// https://developers.dingtalk.com/document/app/obtain-the-number-of-employees
type employee_count_back struct {
	Errcode int    `json:"errcode"`
	Count   int    `json:"count"`
	Errmsg  string `json:"errmsg"`
}

// 在职ID列表返回信息
// https://developers.dingtalk.com/document/app/intelligent-personnel-query-the-list-of-on-the-job-employees-of-the
type employe_id_list_back struct {
	Errcode int    `json:"errcode"`
	Errmsg  string `json:"errmsg"`
	Result  struct {
		DataList   []string `json:"data_list"`
		NextCursor int      `json:"next_cursor"`
	} `json:"result"`
	Success   bool   `json:"success"`
	RequestID string `json:"request_id"`
}

//员工花名册返回信息
//https://developers.dingtalk.com/document/app/intelligent-personnel-obtain-employee-roster-information

type employee_hrm_back struct {
	Errcode   int               `json:"errcode"`
	Errmsg    string            `json:"errmsg"`
	Result    []employee_result `json:"result"`
	Success   bool              `json:"success"`
	RequestID string            `json:"request_id"`
}
type employee_result struct {
	CorpID        string `json:"corp_id"`
	FieldDataList []struct {
		FieldCode      string `json:"field_code"`
		FieldName      string `json:"field_name"`
		FieldValueList []struct {
			ItemIndex int    `json:"item_index"`
			Label     string `json:"label"`
			Value     string `json:"value"`
		} `json:"field_value_list"`
		GroupID string `json:"group_id"`
	} `json:"field_data_list"`
	Userid string `json:"userid"`
}

// 部门返回信息
// https://developers.dingtalk.com/document/app/obtain-the-department-list
type department_back struct {
	Errcode    int    `json:"errcode"`
	Errmsg     string `json:"errmsg"`
	Department []struct {
		SourceIdentifier string `json:"sourceIdentifier,omitempty"`
		CreateDeptGroup  bool   `json:"createDeptGroup"`
		Name             string `json:"name"`
		ID               int    `json:"id"`
		AutoAddUser      bool   `json:"autoAddUser"`
		Parentid         int    `json:"parentid"`
	} `json:"department"`
}
